<template>
  <el-dropdown trigger="hover">
    <div class="avatar">
      <img :src="avatarUrl" alt="avatar" />
    </div>
    <template #dropdown>
      <el-dropdown-menu>
        <router-link to="/user/profile">
          <el-dropdown-item>
            <el-icon><User /></el-icon> 个人信息
          </el-dropdown-item>
        </router-link>
        <el-dropdown-item>
          <el-icon><Edit /></el-icon> 修改密码
        </el-dropdown-item>
        <el-dropdown-item divided @click="logout">
          <el-icon><SwitchButton /></el-icon> 退出登录
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script setup lang="ts">
import { LOGIN_URL } from "@/config";
import { ElMessageBox, ElMessage } from "element-plus";
import { useUserStore } from "@/stores/modules/user";

const userStore = useUserStore();

const router = useRouter();
const avatarUrl = userStore.avatar;
// 退出登录
const logout = () => {
  ElMessageBox.confirm("您是否确认退出登录?", "温馨提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(async () => {
    await userStore.logout();
    // 3.重定向到登陆页
    router.replace(LOGIN_URL);
    ElMessage.success("退出登录成功！");
  });
};
</script>

<style scoped lang="scss">
.avatar {
  width: 40px;
  height: 40px;
  overflow: hidden;
  cursor: pointer;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 01);
  img {
    width: 100%;
    height: 100%;
  }
  &:hover {
    border: 1px solid #eaeaea;
  }
}
</style>
